package org.jpro.modules.system.dao;

import java.util.List;

import org.jpro.common.jpa.repository.JpaRepository;
import org.jpro.modules.system.entity.Permission;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

/**
 * <P>功能描述 : 权限 </P>
 * <P>File name : PermissionRepository </P>
 * <P>Author : 王泽浩 </P>
 * <P>E-Mail : 1028625100@qq.com </P>
 * <P>Date : 2017/3/5 上午11:35</P>
 * @since x
 */
@Repository
public interface PermissionRepository extends JpaRepository<Permission, Integer> {

    /**
     * <P>功能描述 : 查询用户拥有的权限</P>
     * <P>Method Name : findPermissions </P>
     * <P>Author : 王泽浩 </P>
     * <P>E-Mail : 1028625100@qq.com </P>
     * <P>Date : 2017-03-05 11:37:44</P>
     * @param userId the user id
     * @return the list
     * @since 1.0
     */
    @Query(value = "select p.* from permission p INNER JOIN role_permission rp on p.ID=rp.PERMISSION_ID INNER JOIN role r ON  r.id=rp.ROLE_ID INNER JOIN user_role  ur ON ur.ROLE_ID =rp.ROLE_ID INNER JOIN user u ON u.id = ur.USER_ID where u.id=:userId order by p.sort",
            nativeQuery = true)
    List<Permission> findPermissions(@Param("userId") Integer userId);

    /**
     * <P>功能描述 : </P>
     * <P>Method Name : findMenus </P>
     * <P>Author : 王泽浩 </P>
     * <P>E-Mail : 1028625100@qq.com </P>
     * <P>Date : 2017-03-05 11:37:44</P>
     * @return the list
     * @since x
     */
    @Query(value = "select p.ID id,p.PID pid,p.NAME name,p.URL url,p.ICON icon,p.SORT sort,p.DESCRIPTION description from permission p where p.TYPE='F' order by p.sort",
            nativeQuery = true)
    List<Permission> findMenus();

    /**
     * <P>功能描述 : </P>
     * <P>Method Name : findMenus </P>
     * <P>Author : 王泽浩 </P>
     * <P>E-Mail : 1028625100@qq.com </P>
     * <P>Date : 2017-03-05 11:37:44</P>
     * @param userId the user id
     * @return the list
     * @since x
     */
    @Query(value = "select p.ID id,p.PID pid,p.NAME name,p.URL url,p.ICON icon,p.SORT sort,p.DESCRIPTION description from permission p INNER JOIN role_permission rp on p.ID=rp.PERMISSION_ID INNER JOIN role r ON r.id=rp.ROLE_ID INNER JOIN user_role ur ON ur.ROLE_ID =rp.ROLE_ID INNER JOIN user u ON u.id = ur.USER_ID where p.TYPE='F' and u.id=:userId order by p.sort",
            nativeQuery = true)
    List<Permission> findMenus(@Param("userId") Integer userId);

    /**
     * <P>功能描述 : </P>
     * <P>Method Name : findMenuOperation </P>
     * <P>Author : 王泽浩 </P>
     * <P>E-Mail : 1028625100@qq.com </P>
     * <P>Date : 2017-03-05 11:37:44</P>
     * @param pid the pid
     * @return the list
     * @since x
     */
    @Query(value = "select p.ID id,p.NAME name,p.URL url,p.PERM_CODE permCode,p.DESCRIPTION description from permission p where p.TYPE='O' and p.PID=:pid order by p.SORT",
            nativeQuery = true)
    List<Permission> findMenuOperation(@Param("pid") Integer pid);
}
